package system;

import gui.DrawingPanel;
import gui.MainPanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JOptionPane;

/**
 * @author Kevin Peters
 */
public class ButtonActionListener implements ActionListener{
	
	MainPanel mainPanel;
	
	public ButtonActionListener(MainPanel mainPanel){
		
		this.mainPanel = mainPanel;
	}
	
	public void actionPerformed(ActionEvent e) {
		Object event = e.getSource();
		JButton currentEventButton;

		if (event instanceof JButton) {
			currentEventButton = (JButton) event;
		} else {
			return;
		}

		if (currentEventButton.getActionCommand().equals(
				SystemProperties.LOADDEFAULTIMAGE)) {
			int response = JOptionPane.showConfirmDialog(mainPanel,
					SystemProperties.CONFIRM_LOADING_DEFAULT_IMAGE, null,
					JOptionPane.YES_NO_OPTION);
			if (response == JOptionPane.YES_OPTION) {
				mainPanel.reloadImage(SystemProperties.RESOURCEPATH
						+ SystemProperties.DEFAULTPICTURENAME, false);
			}

		} else if (currentEventButton.getActionCommand().equals(
				SystemProperties.LOADCUSTOMIMAGE)) {
			int response = JOptionPane.showConfirmDialog(mainPanel,
					SystemProperties.CONFIRM_LOADING_CUSTOM_IMAGE, null,
					JOptionPane.YES_NO_OPTION);
			if (response == JOptionPane.YES_OPTION) {
				String customImagePath = mainPanel.chooseFilePath(false);
				if (customImagePath != null) {
					mainPanel.reloadImage(customImagePath, true);
				}
			}

		} else if (currentEventButton.getActionCommand().equals(
				SystemProperties.SAVE)) {
			String customImagePath = mainPanel.chooseFilePath(true);
			if (customImagePath != null) {
				Map<Integer, DrawingPanel> drawingPanels = mainPanel
						.getMaskGrid().getDrawingPanelMap();
				ImageFactory.saveImage(customImagePath, drawingPanels);
			}
			
		} else if (currentEventButton.getActionCommand().equals(
				SystemProperties.UNDO)) {
			PanelFactory.performUndo();

		} else if (currentEventButton.getActionCommand().equals(
				SystemProperties.REDO)) {
			PanelFactory.performRedo();
		}
	}
}
